home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 97 / CD-ROM 97 / CD-ROM 97.iso / internet / ghostzilla / ghsetup.exe / chrome / comm.jar / content / editor / EdInputProps.js < prev    next >
Encoding:
JavaScript  |  2002-04-09  |  10.3 KB  |  349 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is Input Tag Properties Dialog.
  15.  *
  16.  * The Initial Developer of the Original Code is
  17.  * Neil Rashbrook.
  18.  * Portions created by the Initial Developer are Copyright (C) 2001
  19.  * the Initial Developer. All Rights Reserved.
  20.  *
  21.  * Contributor(s): Neil Rashbrook <neil@parkwaycc.co.uk>
  22.  *
  23.  * Alternatively, the contents of this file may be used under the terms of
  24.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  25.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  26.  * in which case the provisions of the GPL or the LGPL are applicable instead
  27.  * of those above. If you wish to allow use of your version of this file only
  28.  * under the terms of either the GPL or the LGPL, and not to allow others to
  29.  * use your version of this file under the terms of the MPL, indicate your
  30.  * decision by deleting the provisions above and replace them with the notice
  31.  * and other provisions required by the GPL or the LGPL. If you do not delete
  32.  * the provisions above, a recipient may use your version of this file under
  33.  * the terms of any one of the MPL, the GPL or the LGPL.
  34.  *
  35.  * ***** END LICENSE BLOCK ***** */
  36.  
  37. var insertNew;
  38. var inputElement;
  39.  
  40. // dialog initialization code
  41.  
  42. function Startup()
  43. {
  44.   if (!InitEditorShell())
  45.     return;
  46.  
  47.   gDialog = {
  48.     accept:             document.documentElement.getButton("accept"),
  49.     inputType:          document.getElementById("InputType"),
  50.     inputNameDeck:      document.getElementById("InputNameDeck"),
  51.     inputName:          document.getElementById("InputName"),
  52.     inputValueDeck:     document.getElementById("InputValueDeck"),
  53.     inputValue:         document.getElementById("InputValue"),
  54.     inputDeck:          document.getElementById("InputDeck"),
  55.     inputChecked:       document.getElementById("InputChecked"),
  56.     inputSelected:      document.getElementById("InputSelected"),
  57.     inputReadOnly:      document.getElementById("InputReadOnly"),
  58.     inputDisabled:      document.getElementById("InputDisabled"),
  59.     inputTabIndex:      document.getElementById("InputTabIndex"),
  60.     inputAccessKey:     document.getElementById("InputAccessKey"),
  61.     inputSize:          document.getElementById("InputSize"),
  62.     inputMaxLength:     document.getElementById("InputMaxLength"),
  63.     inputAccept:        document.getElementById("InputAccept"),
  64.     MoreSection:        document.getElementById("MoreSection"),
  65.     MoreFewerButton:    document.getElementById("MoreFewerButton"),
  66.     AdvancedEditButton: document.getElementById("AdvancedEditButton"),
  67.     AdvancedEditDeck:   document.getElementById("AdvancedEditDeck")
  68.   };
  69.  
  70.   // Get a single selected input element
  71.   var tagName = "input";
  72.   inputElement = editorShell.GetSelectedElement(tagName);
  73.  
  74.   if (inputElement)
  75.     // We found an element and don't need to insert one
  76.     insertNew = false;
  77.   else
  78.   {
  79.     insertNew = true;
  80.  
  81.     // We don't have an element selected,
  82.     //  so create one with default attributes
  83.  
  84.     inputElement = editorShell.CreateElementWithDefaults(tagName);
  85.     if (!inputElement)
  86.     {
  87.       dump("Failed to get selected element or create a new one!\n");
  88.       window.close();
  89.       return;
  90.     }
  91.  
  92.     var imageElement = editorShell.GetSelectedElement("img");
  93.     if (imageElement)
  94.     {
  95.       // We found an image element, convert it to an input type="image"
  96.       inputElement.setAttribute("type", "image");
  97.  
  98.       var attributes = ["src", "alt", "width", "height", "hspace", "vspace", "border", "align"];
  99.       for (i in attributes)
  100.         inputElement.setAttribute(attributes[i], imageElement.getAttribute(attributes[i]));
  101.     }
  102.     else
  103.       inputElement.setAttribute("value", GetSelectionAsText());
  104.   }
  105.  
  106.   // Make a copy to use for AdvancedEdit
  107.   globalElement = inputElement.cloneNode(false);
  108.  
  109.   InitDialog();
  110.  
  111.   InitMoreFewer();
  112.  
  113.   gDialog.inputType.focus();
  114.  
  115.   SetWindowLocation();
  116. }
  117.  
  118. function InitDialog()
  119. {
  120.   var type = globalElement.getAttribute("type");
  121.   var index = 0;
  122.   switch (type)
  123.   {
  124.     case "button":
  125.       index = 9;
  126.       break;
  127.     case "checkbox":
  128.       index = 2;
  129.       break;
  130.     case "file":
  131.       index = 6;
  132.       break;
  133.     case "hidden":
  134.       index = 7;
  135.       break;
  136.     case "image":
  137.       index = 8;
  138.       break;
  139.     case "password":
  140.       index = 1;
  141.       break;
  142.     case "radio":
  143.       index = 3;
  144.       break;
  145.     case "reset":
  146.       index = 5;
  147.       break;
  148.     case "submit":
  149.       index = 4;
  150.       break;
  151.   }
  152.   gDialog.inputType.selectedIndex = index;
  153.   gDialog.inputName.value = globalElement.getAttribute("name");
  154.   gDialog.inputValue.value = globalElement.getAttribute("value");
  155.   gDialog.inputChecked.setAttribute("checked", globalElement.hasAttribute("checked"));
  156.   gDialog.inputSelected.setAttribute("checked", globalElement.hasAttribute("checked"));
  157.   gDialog.inputReadOnly.setAttribute("checked", globalElement.hasAttribute("readonly"));
  158.   gDialog.inputDisabled.setAttribute("checked", globalElement.hasAttribute("disabled"));
  159.   gDialog.inputTabIndex.value = globalElement.getAttribute("tabindex");
  160.   gDialog.inputAccessKey.value = globalElement.getAttribute("accesskey");
  161.   gDialog.inputSize.value = globalElement.getAttribute("size");
  162.   gDialog.inputMaxLength.value = globalElement.getAttribute("maxlength");
  163.   gDialog.inputAccept.value = globalElement.getAttribute("accept");
  164.   SelectInputType();
  165. }
  166.  
  167. function SelectInputType()
  168. {
  169.   var index = gDialog.inputType.selectedIndex;
  170.   gDialog.AdvancedEditDeck.setAttribute("selectedIndex", 0);
  171.   gDialog.inputNameDeck.setAttribute("selectedIndex", 0);
  172.   gDialog.inputValueDeck.setAttribute("selectedIndex", 0);
  173.   gDialog.inputValue.disabled = false;
  174.   gDialog.inputChecked.disabled = index != 2;
  175.   gDialog.inputSelected.disabled = index != 3;
  176.   gDialog.inputReadOnly.disabled = index > 1;
  177.   gDialog.inputTabIndex.disabled = index == 7;
  178.   gDialog.inputAccessKey.disabled = index == 7;
  179.   gDialog.inputSize.disabled = index > 1;
  180.   gDialog.inputMaxLength.disabled = index > 1;
  181.   gDialog.inputAccept.disabled = index != 6;
  182.   switch (index)
  183.   {
  184.     case 0:
  185.     case 1:
  186.       gDialog.inputValueDeck.setAttribute("selectedIndex", 1);
  187.       gDialog.inputDeck.setAttribute("selectedIndex", 2);
  188.       break;
  189.     case 2:
  190.       gDialog.inputDeck.setAttribute("selectedIndex", 0);
  191.       break;
  192.     case 3:
  193.       gDialog.inputDeck.setAttribute("selectedIndex", 1);
  194.       gDialog.inputNameDeck.setAttribute("selectedIndex", 1);
  195.       break;
  196.     case 6:
  197.       gDialog.inputValue.disabled = true;
  198.       gDialog.inputAccept.disabled = false;
  199.       break;
  200.     case 8:
  201.       gDialog.inputValue.disabled = true;
  202.       gDialog.AdvancedEditDeck.setAttribute("selectedIndex", 1);
  203.       gDialog.inputName.removeEventListener("input", onInput, false);
  204.       break;
  205.     case 7:
  206.       gDialog.inputValueDeck.setAttribute("selectedIndex", 1);
  207.       break;
  208.   }
  209.   onInput();
  210. }
  211.  
  212. function onInput()
  213. {
  214.   var disabled = false;;
  215.   switch (gDialog.inputType.selectedIndex)
  216.   {
  217.   case 3:
  218.     disabled = disabled || !gDialog.inputValue.value;
  219.   case 4:
  220.   case 5:
  221.     break;
  222.   case 8:
  223.     disabled = !globalElement.hasAttribute("src");
  224.     break;
  225.   default:
  226.     disabled = !gDialog.inputName.value
  227.     break;
  228.   }
  229.   if (gDialog.accept.disabled != disabled)
  230.   {
  231.     gDialog.accept.disabled = disabled;
  232.     gDialog.AdvancedEditButton.disabled = disabled;
  233.   }
  234. }
  235.  
  236. function doImageProperties()
  237. {
  238.   window.openDialog("chrome://editor/content/EdImageProps.xul",
  239.                     "_blank", "chrome,close,titlebar,modal", globalElement);
  240.   window.focus();
  241.   onInput();
  242. }
  243.  
  244. function ValidateData()
  245. {
  246.   var attributes = {
  247.     type: "",
  248.     name: gDialog.inputName.value,
  249.     value: gDialog.inputValue.value,
  250.     tabindex: gDialog.inputTabIndex.value,
  251.     accesskey: "",
  252.     size: "",
  253.     maxlength: "",
  254.     accept: ""
  255.   };
  256.   var index = gDialog.inputType.selectedIndex;
  257.   var flags = {
  258.     checked: false,
  259.     readonly: false,
  260.     disabled: gDialog.inputDisabled.checked
  261.   };
  262.   switch (index)
  263.   {
  264.     case 1:
  265.       attributes.type = "password";
  266.     case 0:
  267.       flags.readonly = gDialog.inputReadOnly.checked;
  268.       attributes.size = gDialog.inputSize.value;
  269.       attributes.maxlength = gDialog.inputMaxLength.value;
  270.       break;
  271.     case 2:
  272.       attributes.type = "checkbox";
  273.       flags.checked = gDialog.inputChecked.checked;
  274.       break;
  275.     case 3:
  276.       attributes.type = "radio";
  277.       flags.checked = gDialog.inputSelected.checked;
  278.       break;
  279.     case 4:
  280.       attributes.type = "submit";
  281.       attributes.accesskey = gDialog.inputAccessKey.value;
  282.       break;
  283.     case 5:
  284.       attributes.type = "reset";
  285.       attributes.accesskey = gDialog.inputAccessKey.value;
  286.       break;
  287.     case 6:
  288.       attributes.type = "file";
  289.       attributes.accept = gDialog.inputAccept.value;
  290.       attributes.value = "";
  291.       break;
  292.     case 7:
  293.       attributes.type = "hidden";
  294.       attributes.tabindex = "";
  295.       break;
  296.     case 8:
  297.       attributes.type = "image";
  298.       attributes.value = "";
  299.       break;
  300.     case 9:
  301.       attributes.type = "button";
  302.       attributes.accesskey = gDialog.inputAccessKey.value;
  303.       break;
  304.   }
  305.   for (var a in attributes)
  306.   {
  307.     if (attributes[a])
  308.       globalElement.setAttribute(a, attributes[a]);
  309.     else
  310.       globalElement.removeAttribute(a);
  311.   }
  312.   for (var f in flags)
  313.   {
  314.     if (flags[f])
  315.       globalElement.setAttribute(f, "");
  316.     else
  317.       globalElement.removeAttribute(f);
  318.   }
  319.   return true;
  320. }
  321.  
  322. function onAccept()
  323. {
  324.   if (ValidateData())
  325.   {
  326.     // All values are valid - copy to actual element in doc or
  327.     //   element created to insert
  328.  
  329.     editorShell.CloneAttributes(inputElement, globalElement);
  330.  
  331.     if (insertNew)
  332.     {
  333.       try {
  334.         // 'true' means delete the selection before inserting
  335.         // in case were are converting an image to an input type="image"
  336.         editorShell.InsertElementAtSelection(inputElement, true);
  337.       } catch (e) {
  338.         dump(e);
  339.       }
  340.     }
  341.  
  342.     SaveWindowLocation();
  343.  
  344.     return true;
  345.   }
  346.   return false;
  347. }
  348.  
  349.